#!/bin/sh
# torque.setup

# USAGE:  torque.setup <USERNAME> [<HOSTNAME>]

if [ "$1" = "" ] ; then
  echo "USAGE:  torque.setup <USERNAME> [<HOSTNAME.DOMAIN>]"
  exit 1
  fi

unset PBSDEBUG
TRQ_CHECK=`pgrep trqauthd`
TRQ_CHECK_RES=$?
if [ "$TRQ_CHECK_RES" = "1" ]; then
  TRQ_START=`trqauthd`
  TRQ_START_RES=$?
  if [ "$TRQ_START_RES" -ne "0" ]; then
    echo "trqauthd failed to start!!! exiting setup"
    exit 1
  else
    echo "trqauthd successfully started"
  fi
fi

if [ "$2" = "" ] ; then
  # obtain name obtained from reverse lookup of ip address
  HOSTNAME=`hostname`
else
  HOSTNAME=$2
fi

# enable operator privileges

USER=$1@$HOSTNAME

echo "initializing TORQUE (admin: $USER)"

ps -ef | grep -v grep | grep pbs_server

if [ "$?" -eq "0" ] ; then
  echo "ERROR: pbs_server already running... run 'qterm' to stop pbs_server and rerun"
  exit 1;
fi

pbs_server -f -t create
# Starting in TORQUE 3.1 the server is multi-threaded.
# We need to pause a second to allow the server to finish coming
# up. If we go to qmgr right away it will fail.
sleep 5

ps -ef | grep -v grep | grep pbs_server

if [ "$?" -ne "0" ] ; then
  echo "ERROR: pbs_server failed to start, check syslog and server logs for more information"
  cat /var/log/messages
  exit 1;
fi

echo set server operators += $USER | qmgr

if [ "$?" -ne "0" ] ; then
  echo "ERROR: cannot set TORQUE admins"
  cat /var/log/messages
  qterm
  exit 1;
fi

echo set server managers += $USER | qmgr


qmgr -c 'set server scheduling = true'
qmgr -c 'set server keep_completed = 300'
qmgr -c 'set server mom_job_sync = true'

# create default queue

qmgr -c 'create queue batch'
qmgr -c 'set queue batch queue_type = execution'
qmgr -c 'set queue batch started = true'
qmgr -c 'set queue batch enabled = true'
qmgr -c 'set queue batch resources_default.walltime = 1:00:00'
qmgr -c 'set queue batch resources_default.nodes = 1'

qmgr -c 'set server default_queue = batch'
